import { RegisterComponent } from "@feng3d/ecs";
import { BoxHelper, Color, Object3D } from "../../thirdparty/three/imports";
import { Component3D } from "../core/Component3D";
import { Node3D } from "../core/Node3D";
import { LineSegments3D } from "../objects/LineSegments3D";
import { Mesh3D } from "../objects/Mesh3D";
import { Scene3D } from "../scenes/Scene3D";

declare module "@feng3d/ecs"
{
    interface ComponentMap { BoxHelper3D: BoxHelper3D; }
}

/**
 * 包装`three`中`BoxHelper`。
 */
@RegisterComponent({ name: 'BoxHelper3D' })
export class BoxHelper3D extends Component3D
{
    _helper: BoxHelper;

    object: Mesh3D | LineSegments3D | Node3D | Scene3D;
    color?: Color;

    init()
    {
        super.init();

        let object: Object3D;

        if (this.object instanceof Node3D)
        {
            object = this.object._group;
        }
        else if (this.object instanceof Mesh3D)
        {
            object = this.object._mesh;
        }
        else if (this.object instanceof Scene3D)
        {
            object = this.object._scene;
        }
        else
        {
            object = this.object._line;
        }

        // 更新全局矩阵
        object.updateWorldMatrix(true, false);

        this._helper = new BoxHelper(object, this.color);

        this._entity._group.add(this._helper);
    }

    destroy(): void
    {
        this._entity._group.remove(this._helper);

        this._helper.dispose();
        this._helper = null;

        super.destroy();
    }
}
